package com.tom.demo.valid.parentheses;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class ValidParentheses2 {
    private static final Map<Character, Character> map = new HashMap<Character, Character>() {{
        put(')', '(');
        put(']', '[');
        put('}', '{');
    }};

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsValue(c)) {
                stack.push(c);
                continue;
            }

            if (!stack.isEmpty() && map.get(c).equals(stack.pop())) {
                continue;
            }
            return false;
        }
        return stack.isEmpty();
    }


    /**
     * ()
     * ()[]{}
     * (]
     * ([])
     * ([)]
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(isValid("()"));
        System.out.println(isValid("()[]{}"));
        System.out.println(isValid("(]"));
        System.out.println(isValid("([])"));
        System.out.println(isValid("([)]"));
    }
}
